package utils;

import graphics.DebugDrawable;
import graphics.Drawable;
import ia.NodeMap;

import java.awt.Graphics;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Vector;

import bots.BotManager;

import surface.Surface;

import item.BotSpawn;

public class Map implements Serializable, Drawable, DebugDrawable {
	private static final long serialVersionUID = 1L;
	
	private NodeMap nodeMap;
	private Surface surface;
	
	private Vector<ArrayList<BotSpawn>> botSpawnLists;
	
	private int teamCount;
	
	public Map(NodeMap nodeMap, Surface surface, int teamCount) {
		this.nodeMap = nodeMap;
		this.surface = surface;
		this.teamCount = teamCount;
		
		botSpawnLists = new Vector<ArrayList<BotSpawn>>(teamCount);
		botSpawnLists.setSize(teamCount);
		
		for(int i = 0; i < teamCount; ++i) {
			botSpawnLists.set(i, new ArrayList<BotSpawn>());
		}
	}
	
	public NodeMap getNodeMap() {
		return nodeMap;
	}
	
	public Surface getSurface() {
		return surface;
	}
	
	public int getTeamCount() {
		return teamCount;
	}
	
	public void setTeamCount(int teamCount) {
		this.teamCount = teamCount;
		
		//Il faut modifier la taille de la liste en cons�quence
		//Et donc la recopier
		Vector<ArrayList<BotSpawn>> tmpList = botSpawnLists;
		botSpawnLists = new Vector<ArrayList<BotSpawn>>(teamCount);
		botSpawnLists.setSize(teamCount);
		
		for(int i = 0; i < teamCount; ++i) {
			botSpawnLists.set(i, new ArrayList<BotSpawn>());
			
			if(i < tmpList.size()) {
				for(int j = 0; j < tmpList.get(i).size(); ++j) {
					botSpawnLists.get(i).add(tmpList.get(i).get(j));
				}
			}
		}
	}
	
	public ArrayList<BotSpawn> botSpawnList(int team) {
		return botSpawnLists.get(team);
	}
	
	public Vector<ArrayList<BotSpawn>> allBotSpawns() {
		return botSpawnLists;
	}

	public void createBotSpawn(BotManager botman, Vector2d startCoord, int team) {
		botSpawnLists.get(team).add(new BotSpawn(botman, startCoord, team));
	}

	public void createBotSpawn(Vector2d startCoord, int team) {
		botSpawnLists.get(team).add(new BotSpawn(startCoord, team));
	}
	
	public void drawDebug(Graphics g) {
		// TODO Auto-generated method stub
		for(ArrayList<BotSpawn> botSpawnList : botSpawnLists) {
			for(BotSpawn botSpawn : botSpawnList) {
				botSpawn.drawDebug(g);
			}
		}
		
		nodeMap.drawDebug(g);
	}
	
	public void draw(Graphics g) {
		// TODO Auto-generated method stub
		for(ArrayList<BotSpawn> botSpawnList : botSpawnLists) {
			for(BotSpawn botSpawn : botSpawnList) {
				botSpawn.draw(g);
			}
		}
		
		surface.draw(g);
	}
}
